﻿#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using System.ComponentModel;
#endregion

namespace TunnelsOfDeath
{
    partial class Game1 : Game
    {
        protected void MainStateGame(GameTime _gt)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();
            if (Keyboard.GetState().IsKeyDown(Keys.K))
            {
                try
                {

                    Globals.ObjectManager.UnregisterObject(Globals.MyCameraManager[1].FollowedPlayer);
                }
                catch
                {


                }


            }

            UpdateTick(_gt);
            Globals.MyCameraManager.CleanUp();


            UpdateMap(_gt);
        }

        protected void UpdateAll(GameTime gameTime)
        {

            double tag = gameTime.TotalGameTime.TotalMilliseconds;

            for (int i = Globals.PositionUpdate.Count - 1; i > 0; i--)
            {
                if (i > Globals.PositionUpdate.Count - 1)
                    continue;
                Globals.PositionUpdate[i].UpdatePosition(tag);
            }

            Globals.MyMapManager.IsPositionListUpdated = false;

        }

        protected void UpdateTick(GameTime gameTime)
        {

            foreach (Objects.Sprite item in Globals.Animated)
            {
                item.Update(gameTime);
            }

            foreach (ViewPosition item in Globals.MyCameraManager)
            {
                item.Update();
            }

        }

        private void UpdateMap(GameTime gameTime)
        {
            if (MainTimer < gameTime.TotalGameTime.TotalMilliseconds)
            {
                MainTimer = gameTime.TotalGameTime.TotalMilliseconds + Globals.SPEED;
                UpdateAll(gameTime);

            }
            else
            {
                Globals.MyMapManager.SortList();
                UpdateGameState();
                //System.Diagnostics.Debug.WriteLine("PositionList sorted, counting " + Globals.PositionUpdate.Count + " objects.");
            }
        }

        private void UpdateGameState()
        {


            System.Diagnostics.Debug.WriteLine(Globals.ActivePlayersOnMap.Count);
            if (Globals.ActivePlayersOnMap.Count != 0 && Globals.CurrentState == GameState.game)
            {
                bool allNotOnMap = true;
                bool allFinished = true;
                foreach (Objects.Player item in Globals.ActivePlayersOnMap)
                {
                    if (item.State != Objects.PlayerState.FINISHED)
                        allFinished = false;
                    if (item.State != Objects.PlayerState.NOTONMAP)
                        allNotOnMap = false;
                }

                if (allNotOnMap)
                {
                    Globals.MainPlayer.Lives--;

                    if (Globals.MainPlayer.Lives > 0)
                    {
                        Globals.StateManager.ChangeState(GameState.levellost);
                    }
                    else
                    {
                        Globals.CurrentState = GameState.gameover;
                    }
                    
                }
                else if (allFinished)
                {
                    Globals.CurrentState = GameState.levelpassed;
                }
            }

        }
    }

}
